package vsvm.gui.forms;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JRadioButton;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;

import vsvm.data.filter.ranking.EntropyMeasure;

import javax.swing.JCheckBox;

import javax.swing.JTextField;
import java.awt.Insets;

public class EntropyMeasureForm extends JDialog {

	private static final long serialVersionUID = 100L;
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JRadioButton btnE = null;
	private JRadioButton btnH = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;

	private EntropyMeasure entropy;
	private EntropyMeasureForm me;
	private JPanel jPanel1 = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JCheckBox jCheckBox = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField = null;
	
	public EntropyMeasureForm(Frame owner, EntropyMeasure e) {
		super(owner);
		this.me = this;
		entropy = e;
		initialize();
		setLocationRelativeTo(owner);
		this.setVisible(true);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(368, 168);
		this.setTitle("Entropy Measure Form");
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel1(), BorderLayout.SOUTH);
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.BOTH;
			gridBagConstraints5.gridy = 2;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints5.gridx = 2;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 1;
			gridBagConstraints4.insets = new Insets(0, 10, 5, 0);
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 2;
			jLabel2 = new JLabel();
			jLabel2.setText("Find n the least important and stop");
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.gridx = 0;
			gridBagConstraints31.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints31.gridy = 2;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.insets = new Insets(0, 10, 5, 0);
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("Hamming distance");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.insets = new Insets(0, 10, 5, 0);
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Euclidean distance");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints1.gridy = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints.gridy = 0;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.setBorder(BorderFactory.createTitledBorder(null, "Entropy measure", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			jPanel.add(getBtnE(), gridBagConstraints);
			jPanel.add(getBtnH(), gridBagConstraints1);
			jPanel.add(jLabel, gridBagConstraints2);
			jPanel.add(jLabel1, gridBagConstraints3);
			jPanel.add(getJCheckBox(), gridBagConstraints31);
			jPanel.add(jLabel2, gridBagConstraints4);
			jPanel.add(getJTextField(), gridBagConstraints5);
		}
		return jPanel;
	}

	/**
	 * This method initializes btnE	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getBtnE() {
		if (btnE == null) {
			btnE = new JRadioButton();
			
			if(!entropy.isHammingDistance())
			{
				btnE.setSelected(true);	
			}
			btnE.addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
				public void mouseClicked(java.awt.event.MouseEvent e) {
					btnE.setSelected(true);
					btnH.setSelected(false);					
				}
			});
		}
		return btnE;
	}

	/**
	 * This method initializes btnH	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getBtnH() {
		if (btnH == null) {
			btnH = new JRadioButton();
			if(entropy.isHammingDistance())
			{
				btnH.setSelected(true);	
			}
			btnH.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					btnE.setSelected(false);
					btnH.setSelected(true);
				}
			});
		}
		return btnH;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(new FlowLayout());
			jPanel1.add(getJButton(), null);
			jPanel1.add(getJButton1(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("    Ok    ");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(btnH.isSelected())
					{
						entropy.setHammingDistance(true);
					}						
					else
					{
						entropy.setHammingDistance(false);
					}
					
					if(jCheckBox.isSelected())
					{
						try 
						{
							int leastImportant = Integer.parseInt(jTextField.getText());
							entropy.setLeastImportant(leastImportant);
						} catch (NumberFormatException e1) {
							JOptionPane.showMessageDialog(me, "Incorrect number" +
									" of the least important attribute!", "Error", 0);
							return;
						}
					}
					me.dispose();
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Cancel");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					me.dispose();
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox();
			if(entropy.getLeastImportant() > 0)
			{
				jCheckBox.setSelected(true);
			}
		}
		return jCheckBox;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			
			if(entropy.getLeastImportant() > 0)
			{
				jTextField.setText(String.valueOf(entropy.getLeastImportant()));
			}
		}
		return jTextField;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
